/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package classcreator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author Stefano
 */
public class Transition extends Method{
    private static double outputFormatVersion=1.0;
    
    public Vector<TransitionInfo> transinfo = new Vector<TransitionInfo>();
    
    public Transition(MethodHeader h){
        super(h);
    }
    
    public Transition(MethodHeader h, int s, int e){
        super(h);
        transinfo.add(new TransitionInfo(s, e));
    }
    
    public Transition(MethodHeader h, int s, int e, String guard){
        super(h);
        transinfo.add(new TransitionInfo(s, e, guard));
    }
    
    public void addTransitionInfos(Vector<TransitionInfo> tris){
        boolean found=false;
        for(TransitionInfo ti:tris){
            found=false;
            for(TransitionInfo iti:transinfo){
                if(ti.equals(iti)){
                    found=true;
                    break;
                }
            }
            if(!found) transinfo.add(ti);
        }
    }
    
    public void addTransitionInfo(TransitionInfo ti){
        boolean found=false;
            for(TransitionInfo iti:transinfo){
                if(ti.equals(iti)){
                    found=true;
                    break;
                }
            }
            if(!found) transinfo.add(ti);
    }
    
    public String toString(){
        Vector<String> oldBody=(Vector<String>)body.clone();
        for(TransitionInfo ti:transinfo){
            String s=Naming.tab+"if("+Naming.stateMachineGetState+"("+ti.start+")){\n";
            if(ti.isGuarded()){
                s=s+Naming.tab+"if( /* "+ti.guard+" */ ){\n";
            }
            s=s+Naming.tab+Naming.tab+Naming.stateMachineSetState+"("+ti.end+");\n"+Naming.tab+Naming.tab+"return;\n";
            if(ti.isGuarded()){
                s=s+Naming.tab+"}\n";
            }
            s=s+Naming.tab+"}";
            body.add(s);
        }
        
        //String ret=super.toString();
        String s=header.toString();
        s=s+"{"+"\n";
        if(!header.returnType.equals(Naming.voidType)&&!header.returnType.equals(Naming.constructorType)){
            s=s+Naming.tab+header.returnType.toString()+" "+Naming.standardReturnName+";\n";
        }
        for(String line:body){
            s=s+line+"\n";
        }
        if(!header.returnType.equals(Naming.voidType)&&!header.returnType.equals(Naming.constructorType)){
            s=s+Naming.tab+"return "+Naming.standardReturnName+";\n";
        }
        s=s+"}";
        
        body=oldBody;
        return s;
    }
    
    public static Transition parse(BufferedReader br, TypeRepository tr) throws IOException{
                br.readLine();
                
                double version=Double.parseDouble(br.readLine());
                
                if(version<=1.0){
                    MethodHeader mh=MethodHeader.parse(br, tr);
                    int n=Integer.parseInt(br.readLine());
                    Transition m=new Transition(mh);
                    for(int i=0; i<n; i++){
                        m.addBodyLine(br.readLine());
                    }
                    n=Integer.parseInt(br.readLine());
                    for(int i=0; i<n; i++){
                        m.addTransitionInfo(TransitionInfo.parse(br, tr));
                    }
                    return m;
                }
                //Actual version
                MethodHeader mh=MethodHeader.parse(br, tr);
                int n=Integer.parseInt(br.readLine());
                Transition m=new Transition(mh);
                for(int i=0; i<n; i++){
                    m.addBodyLine(br.readLine());
                }
                n=Integer.parseInt(br.readLine());
                for(int i=0; i<n; i++){
                    m.addTransitionInfo(TransitionInfo.parse(br, tr));
                }
                return m;
    }
    
    public void permanentize(BufferedWriter br, TypeRepository tr) throws IOException{
        br.write("/***/ Transition: "+header.toString()); br.newLine();
        br.write(String.valueOf(outputFormatVersion)); br.newLine();
                header.permanentize(br,tr); 
                br.write(String.valueOf(body.size())); br.newLine();
                for(String s:body){
                    br.write(s); br.newLine();
                }
                br.write(String.valueOf(transinfo.size())); br.newLine();
                for(TransitionInfo t:transinfo){
                    t.permanentize(br, tr); 
                }
    }
}
